import { Component, OnInit, ViewChild } from '@angular/core';
import { PageModel } from "../../../../model/page-model";
import { ModalDirective } from 'ng2-bootstrap/modal';
import { flyIn } from '../../../../animations/fly-in';
import { MyHttp } from '../../../../service/MyHttp';

@Component({
  selector: 'generalusers',
  templateUrl: './generalusers.component.html',
  styleUrls: ['./generalusers.component.css'],
  animations: [
    flyIn
  ]
})
export class Generalusers implements OnInit {
  @ViewChild('myModal') public myModal: ModalDirective;
  pageModel = new PageModel();
  page: number = 1;
  comfirmpassword: any;
  userdata: any;
  btype: number;
  public selectedUser;
  public keyWord: string = '';
  private key_word: string = '';
  constructor(private myHttp: MyHttp) { }
  ngOnInit() {
    this.getData(1);
  }

  getData(page) {
    this.page = page;
    this.myHttp.post('user/findByPage?page=' + this.page + '&key_word=' + this.key_word, {}).subscribe(res => {
      if (res.errorCode == 0) {
        this.userdata = res.message.data;
        this.pageModel.setPage({ 'iTotalRecords': res.message.total });
      }
    }, err => console.log(err));
  }

  search(key_word) {
    this.key_word = key_word;
    this.getData(1);
    this.pageModel.currentPage = 1;
  }

  userLimit(){
    this.myHttp.post('user/limitUser',{user_telephone:this.selectedUser.user_telephone,status:1-this.selectedUser.status}).subscribe(res=>{
      if(res.errorCode == 0){
        alert('用户'+(this.selectedUser.status==0?'开启':'禁用')+'成功');
        this.getData(this.page);
        this.cancel();
      }
      else{
        alert(res.message);
      }
    });
  }

  cancel() {
    this.myModal.hide();
  }
}

